$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					if (totalCount == null || totalCount == ""
							|| totalCount == "undefined") {
						totalCount = 0;

					}
					if (pSize == null || pSize == "" || pSize == "undefined") {
						pSize = 10;

					}

					
					if (pageCount == null || pageCount == ""
							|| pageCount == "undefined") {
						pageCount = 1;

					}
                   

					$('#pagination2')
					.jqPaginator(
							{
								
								totalPages : pageCount,
								visiblePages : 5,
								currentPage : 1,


								prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
								page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
								next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
								onPageChange : function(num, type) {
									 
									$("#pageIndex").val(num);
									var roleName=$("#roleName").val();
									
									var queryUrl = "queryByPage.do";
									$.ajax({
												url : queryUrl,
												dataType : 'json',
												data : {
													pageIndex : num,
													pageSize : pSize,
													roleName:roleName
												},
												cache : false,
												success : function(data) {

													$("#products tr:not(:first)").remove();
													var results = data.pageResult.resultList;
													for (i = 0; i < results.length; i++) {
														var role = results[i];

														var rowContent = "<tr>"
																+ "<td><input name='proId' class='checkbox' type='checkbox' value='"
																+ role.id
																+ "'/></td>"

																+ "<td>"
																+ role.rCode
																+ "</td>"
																+ "<td>"
																+ role.rName
																+ "</td>"
																+ "<td>"
																+ role.note
																+ "</td>"

														rowContent = rowContent
																+ "<td><button name='editBtn' objId='"
																+ role.id
																+ "' class='btn btn-sm btn-primary'>修改</button></td>"
																+ "<td><button name='viewUser' objId='"
																+ role.id
																+ "'"
																+ "class='btn btn-sm btn-danger'>查看用户列表</button></td>"

																+ +"</tr>";
														$("#products")
																.append(
																		rowContent);
													}

												},
												error : function(html) {
													alert("error"+html);
													return;
												}
											});
								}
							});
					
					
					
					
					$('form')
							.submit(
									function() {
										if (typeof jQuery.data(this,
												"disabledOnSubmit") == 'undefined') {
											jQuery.data(this,
													"disabledOnSubmit", {
														submited : true
													});
											$(
													'input[type=submit], input[type=button]',
													this).each(
													function() {
														$(this).attr(
																"disabled",
																"disabled");
													});
											return true;
										} else {
											return false;
										}
									});

					$("#optBtn").delegate(
							'#saveBtn',
							'click',
							function() {

								$("#productDetail").attr("method", "post");

								var proId = $(
										"#productDetail input[id='proId']")
										.val();
								// alert(proId);
								if (proId == "" || proId == null
										|| proId == "undefined") {

									$("#productDetail").attr("action",
											"save.do");
									alert("save");
								} else {
									$("#productDetail").attr("action",
											"update.do");
									alert("update");
								}
								$("#productDetail").submit();

							});



					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='proId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#products").delegate('[name="editBtn"]', 'click',
							function() {
								var roleId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : roleId

									},
									cache : false,

									success : function(data) {

										loadRole(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#products").delegate('[name="viewResource"]', 'click',
							function() {
								var proId = $(this).attr("objId");
								alert("////////proId=" + proId);

								loadResource(proId, 0);
							});

					$("#products").delegate(
							'[name="viewRule"]',
							'click',
							function() {
								var proId = $(this).attr("objId");

								var editUrl = basePath
										+ "/manage/product/proDetails/list.do";
								document.location.href = editUrl + "?id="
										+ proId;
							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="proId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								var ids = "";
								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									type : "POST",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					$("#rName")
							.delegate(
									$(this),
									'change',
									function() {

										var rName = $(this).val();

										queryUrl = encodeURI("queryRolePinYin.do?rName="+ rName + "");
										if (rName !=null && rName != "" && rName.length > 0) {
											$.ajax({
												url : queryUrl,
												dataType : 'text',

												cache : false,

												success : function(data) {

													$("#rCode").val(data)
												},
												error : function(data) {
													alert("获取角色编号出错");
												}
											});
										} else {
											alert("请输入角色名称");
										}
									});

					$("#tools").delegate('#addProduct', 'click', function() {

						
						var editUrl = "add.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							
							cache : false,

							success : function(data) {

								// alert("************"+data.typeId);
								$("#productDialog input").each(function() {
									$(this).prop("value", null);
									$(this).prop("readonly", false);

								});
								$("#productDialog textarea").each(function() {
									$(this).prop("value", null);

								});

								loadRole(data)

							},
							error : function(html) {
								alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});

					function loadResource(proId, type) {

						$("#resourceDialog input[id='proId']").val(proId);
						var idx = 0;
						$("#baseTab").addClass("active");
						$("#pictrues").addClass("active");
						$("#itemsTab").removeClass("active");
						$("#videos").removeClass("active");
						$("#resourceDialog").modal('show');

					}

					function loadRole(role) {

						$("#proId").val(role.id);
						$("#rCode").val(role.rCode);
						$("#rName").val(role.rName);
						$("#note").val(role.note);
						$("#productDialog").modal("show");
					}

					function vaildate(form) {
						return true;
					}

					function refresh() {
						window.location.reload();
					}
				});
